package com.mdb.open.trade.event.listener;

import com.mdb.bc.utils.BeanConvertUtil;
import com.mdb.open.trade.domain.po.product.LimitInfoHis;
import com.mdb.open.trade.domain.po.product.LimitUserProd;
import com.mdb.open.trade.domain.po.product.LimitUserProdHis;
import com.mdb.open.trade.event.model.LimitInfoHisEvent;
import com.mdb.open.trade.event.model.LimitUserProdHisEvent;
import com.mdb.open.trade.service.product.LimitInfoHisService;
import com.mdb.open.trade.service.product.LimitUserProdHisService;
import com.mdb.open.trade.service.product.LimitUserProdService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author liuyi
 * @date 2019/08/23
 **/
@Log4j2
@Component
public class LimitInfoEventListener {

    @Autowired
    private LimitInfoHisService limitInfoHisService;
    @Autowired
    private LimitUserProdService limitUserProdService;
    @Autowired
    private LimitUserProdHisService limitUserProdHisService;


    @EventListener
    public void test(LimitInfoHisEvent event){
        LimitInfoHis limitInfoHis = BeanConvertUtil.convertObject(event.getLimitInfo(), LimitInfoHis.class);
        limitInfoHis.setId(null);
        limitInfoHis.setUpdateTime(LocalDateTime.now());
        boolean save = limitInfoHisService.save(limitInfoHis);
        if(save) log.info("保存额度变更历史成功。额度编号：{}", event.getLimitInfo().getLimitNo());
        else log.error("保存额度变更历史失败。额度编号：{}", event.getLimitInfo().getLimitNo());
    }

    @EventListener
    public void test(LimitUserProdHisEvent event){
        //修改商户额度产品表
        LimitUserProd updateLimitUserProd = new LimitUserProd();
        updateLimitUserProd.setId(event.getLimitUserProd().getId());
        updateLimitUserProd.setUserName(event.getUpdateMerchantLimitDto().getMerchantName());
        boolean updateCount = limitUserProdService.updateById(updateLimitUserProd);
        if(updateCount) log.info("修改商户额度信息成功。额度编号：{}", event.getUpdateMerchantLimitDto().getMerchantNo());
        else log.error("修改商户额度信息失败。额度编号：{}", event.getUpdateMerchantLimitDto().getMerchantNo());

        LimitUserProdHis limitUserProdHis = BeanConvertUtil.convertObject(event.getLimitUserProd(), LimitUserProdHis.class);
        limitUserProdHis.setId(null);
        boolean saveCount = limitUserProdHisService.save(limitUserProdHis);
        if(saveCount) log.info("保存商户产品额度变更历史成功。额度编号：{}", event.getLimitUserProd().getLimitNo());
        else log.error("保存商户产品额度变更历史失败。额度编号：{}", event.getLimitUserProd().getLimitNo());
    }

}
